clear all;
close all;
clc;

cd('/home/andrey/svn_local/mri-tut/code/');
%%% Will work on the xwin experiment results firstly
%% go to the correct working dir
cd('../NMR_tutorial_200911');
% first of all we'll work with the first experiment:
%% opening results file
fid_ser = fopen('./1/ser', 'r', 'b');
fid_mat = fread(fid_ser, 'int32');
fid_mat_size = size (fid_mat);
ones_mat = logical(ones(fid_mat_size)');
odd_mat = logical(mod(1:fid_mat_size(1),2));
even_mat = logical(ones_mat - odd_mat);
real_fid = fid_mat(odd_mat);
im_fid = fid_mat(even_mat);
fid_mat_comp = complex(real_fid, im_fid);
fclose(fid_ser);
dots_num_exp = size(fid_mat_comp, 1);
fid_mat_comp_exp = reshape(fid_mat_comp, dots_num_exp/40, 40);

abs_fid_exp = abs(fid_mat_comp_exp);
real_fid_exp = real(fid_mat_comp_exp);
im_fid_exp = imag(fid_mat_comp_exp);
figure;
title ('Inversion recovery experiment')
hold on;
for i =1:20:40
    plot (abs_fid_exp(:,i), 'k');
    plot (real_fid_exp(:,i), 'r');
    plot (im_fid_exp(:,i), 'b');
end
legend('Abs(fid)', 'Real(fid)', 'Im(fid)');

fid_fft = fftshift(fft(fid_mat_comp_exp));
figure;
hold on;
title('FFT of Inversion recovery');
for i =1:20:40
    plot (real(fid_fft(:,i)), 'k');
    plot (imag(fid_fft(:,i)), 'r');
end
legend ('real(fft(fid))', 'imag(fft(fid))')

%%  let us find the points in 4 ways:
%%  1 - maximum point (abs)
%%  2 - maximum point (real)
%%  3 - maximum point (fft)
%%  (As a possibility we can perform an average between 3 maximal points in each one of the cases)
%%  4 - signal integral (fft)

T1_mat = zeros(4, 40);
T1_mat(1,:) = max (abs_fid_exp);
T1_mat(2,:) = max (real_fid_exp);
T1_mat(3,:) = max (real(fid_fft));
T1_mat(4,:) = sum (abs(real(fid_fft)));




for i = 1:4
    figure;
    plot(100e-6:100e-6:4000e-6, T1_mat(i,:));
end